package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;



    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
//todo 1. 查询公寓信息 this代指当前要查询的对象
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
        // 判断公寓信息是否存在
        if (apartmentInfo == null) {
            return null;
        }
        //todo 2. 查询公寓所对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoListByItemTypeAndItemId(ItemType.APARTMENT, id);
        //todo 3. 查询公寓所对应的配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByApartmentId(id);
        //todo 4. 查询公寓所对应的标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoListByApartmentId(id);
        //todo 5. 查询公寓最小租金
        BigDecimal minRent = apartmentInfoMapper.selectMinRentByApartmentId(id);
        //todo 6. 封装返回结果
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //todo 6.1 将公寓信息复制到公寓详情中 属性复制 前提 ： 属性名一致
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        //todo 6.2 将图片信息、配套信息、标签信息、最小租金
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setMinRent(minRent);

        //todo 7. 返回结果
        return apartmentDetailVo;
    }

    @Override
    public ApartmentItemVo selectApartmentItemVoById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

//        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
//        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;

    }


}




